home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / J4FTUT01.ZIP / temp / techtut / tut1 / ptech1.txt next >
Encoding:
Text File  |  1997-09-10  |  12.2 KB  |  318 lines

  1.  
  2.  ========================================================================  
  3.                    Just4Fun Productions presents:
  4.    
  5.                       Part 1 of: the Techtutor
  6.                        (Topics never covered)  
  7.  
  8.  
  9.                            Bullets Handling
  10.  ========================================================================  
  11.                   
  12. Introduction
  13. ============-
  14. Welcome to the first of these tech files... These files were originally only 
  15. on the homepage of Just4Fun Productions, but I tought they deserved to have 
  16. a more public audience, so here it is!
  17. Since I allready had these files completed, the first few are released on 
  18. the same date, so if you downloaded this from some place, you should be able 
  19. to get the next few at that same place (I uploaded them atleast to x2ftp).
  20.  
  21. The topics I'll cover are not the normal demo tutors, or vga trainers (get 
  22. the tutors from Telemachos and Denthor for that, they'r good!)
  23. I'll cover the game-programming topics that are never explained (at least 
  24. not to my knowledge...), things I'll cover are for some simple things, but 
  25. for alot of other people very difficult to do. Most topics are useable in 2D 
  26. games and 3D games, but since I still like the 2D games my code is also 
  27. focused on those type of games.
  28.  
  29. Most of the code I talk about in these files will also be extracted from the 
  30. SuperFX engine, a freeware engine I created for 2D games...you can download 
  31. it from the Just4Fun homepage (http://people.zeelandnet.nl/rpb).
  32. Also I must tell you that the example code doesn't allways work just like 
  33. that, you might have to add things or change things...the hard stuff is 
  34. usually working though..
  35.  
  36.  
  37. ----------------------------------------------------------------
  38. For contacting Just4Fun or me (PeeBee) use the following ways:
  39.  
  40. Internet : http://people.zeelandnet.nl/rpb
  41. Email    : just4fun@zeelandnet.nl
  42.  
  43. SnailMail: P.Bestebroer
  44.      Anthony Edenlaan 28
  45.      4463 JC  Goes (Zld.)
  46.       Holland
  47.  
  48. ICQ      : 2309110 (probably fastest way to contact me)
  49. ----------------------------------------------------------------
  50.  
  51.  
  52.  
  53.  
  54. Lets Start This!
  55. ================-
  56.  
  57. Now let's talk about bullets, they'r very important in most games
  58. but how could we program them? First let's see what a bullet does in the 
  59. game:
  60.  * It's shot by the player or enemies
  61.  * It flies a certain path until it hits something
  62.  * If it hits an enemy then kill it, and erase the bullet
  63.  * if it hits a wall erase the bullet.
  64.  
  65. This list shows that a bullet is only active for a short period so creating 
  66. an array would not be that efficient.
  67.  
  68.  
  69. We need a linked-list!
  70. ======================-
  71.  
  72. The best way for bullets would be to create a linked-list. For those of 
  73. you who don't know what this is, here's some information:
  74.  
  75. A linked list is composed of pointers. You'll need at least two pointers
  76. one will be the first, pointing to the first item in the list, and
  77. the second will be the last, pointing to...exactly the last item.
  78.  
  79. Now the item it points to can be any variable you want, but using           
  80. a RECORD would be best in the case of bullets.  Here's an example of 
  81. defining the linked-list:
  82.  
  83.                   TYPE PBullet = ^TBullet;
  84.                   TYPE TBullet = record
  85.                        xposition : word;
  86.                        yposition : word;
  87.                        previous  : PBullet;
  88.                        next      : PBullet;
  89.                   END;
  90.  
  91.                   VAR FirstBullet : PBullet;
  92.                       LastBullet  : PBullet;
  93.  
  94. Let's explain this. The first type "PBULLET" is a pointer type of            
  95. the Tbullet record, so that's used for the linked-list. The variables 
  96. xposition/yposition explain them selves, but the previous/next variables are 
  97. the main thing for the linked-list. They point to the previous/next item in 
  98. the linked list! So let's say we've got 5 bullets flying around. 
  99. Our list would look like:
  100.  
  101.       FirstBullet = Bullet(1)
  102.          Bullet(1).Previous = NIL        { No bullet infront of bullet 1 ! }
  103.          Bullet(1).Next = Bullet(2)      { pointer to next bullet          }
  104.          Bullet(2).Previous = Bullet(1)  { The previous bullet is bullet 1 }
  105.          Bullet(2).Next = Bullet(3)      { pointer to next bullet = 3      }
  106.          ....
  107.      LastBullet  = Bullet(5)           { the lastbullet is bullet(5)     }
  108.          Bullet(5).Previous = Bullet(4)  { the previous bullet is bullet 4 }
  109.          Bullet(5).Next = NIL            { there are no more bullets       }
  110.  
  111. That's all the information about "linked-list" I'll give here, just           
  112. find some more and bether examples if you still don't get it (there are 
  113. enough books explaining it, and there should be some nice text files on the 
  114. internet somewhere).
  115.  
  116.  
  117. How do we create the bullets?
  118. =============================-
  119.  
  120. Bullet's will never be "there" all the time, so they have to be             
  121. created as soon as the player hits FIRE, and erase as soon as they          
  122. hit something. For this purpose we'll need a small procedure that will
  123. add a new bullet to the linked-list. The procedure that does this is called 
  124. ADDBULLET, it's only purpose is to "initialise" a new bullet by adding it to 
  125. the linked-list.   Once the bullet is added it will be processed by the 
  126. DOBULLETS procedure. The ADDBULLET procedure needs some information about
  127. the position of the bullet, the person who shot it (player/enemy)             
  128. the BULLET-AI (ie. a rocket, shell, grenade, etc...). The speed of the 
  129. bullet, the image-frame (sprite) the bullet uses, and maybe some specific 
  130. things for you'r game, but that's upto you.
  131.  
  132. {=-=-= Example =-=-=}
  133. FUNCTION AddBullet(  Xpos2,Ypos2,xspeed2,yspeed2  : integer;
  134.                      F_Frame2,ai2,subai2          : byte):boolean;
  135. VAR  TempBull : PBullet;    { this is used to create the new-bullet }
  136. BEGIN
  137.  AddBullet:=false;  { No bullet is added upto this point }
  138.  
  139.  new(TempBull);              { Get memory for new bullet }
  140.  
  141.  if TempBull=nil then exit;  { if TEMP=NIL, we don't have enough memory }
  142.  
  143.  if FirstBullet=NIL then begin { is the list empty? }
  144.     LastBullet:=TempBull;       { yes, so the NEW-BULLET is the first+last }
  145.     FirstBullet:=TempBull;
  146.     with FirstBullet^ do begin
  147.          next:=NIL;             { next/previous are nothing, because there }
  148.          prev:=Nil;             { is only one bullet in the list }
  149.     end;
  150.   end else begin                 { More bullets in the list! }
  151.      LastBullet^.next:=tempbull;{ Lastbullet points to new-bullet }
  152.      TempBull^.prev:=LastBullet;{ lastbullet is not lastbullet anymore }
  153.      LastBullet:=TempBull;      { Lastbullet=NEWBULLET }
  154.      Tempbull^.next:=NIL;       { no bullets after the new-bullet }
  155.   end;
  156.  
  157.   With TempBull^ do begin        { Set the new values }
  158.    xpos:=xpos2+xspeed2;
  159.    ypos:=ypos2+yspeed2;
  160.    xspeed:=xspeed2;
  161.    yspeed:=yspeed2;
  162.  
  163.    ai       :=ai2;
  164.    subai    :=subai2;
  165.    f_Frame  :=f_Frame2;
  166.   end;
  167.  
  168.   AddBullet:=True;               { the bullet was added }
  169. END;
  170. {=-=-= End of Example =-=-=-=}
  171.  
  172. What the ADDBULLET procedure does is this:
  173.  * Create a new pointer to a bullet-type
  174.  * Update LASTBULLET and FIRSTBULLET pointers if necessary.
  175.  * set the new-bullet variables to the ones specified (xpos, ypos,speed...)
  176. Now a new bullet is added to the linked-list...
  177.  
  178.  
  179. I believe it can fly
  180. ====================-
  181.  
  182. Now we have a bullet in the list, but we still have to make it work.
  183. To do this, we call the DOBULLETS procedure every frame. What the DOBULLETS 
  184. procedure does is:
  185.  
  186.  * Get pointer of FIRSTBULLET.
  187.  * Update Xposition, Yposition, animation, energy, etc...
  188.  * Check if bullet hits something, and take action
  189.  * Draw bullet on screen
  190.  * Go to next bullet in the list, until LASTBULLET is reached.
  191.  
  192. {=-=-= Example =-=-=-=}
  193. PROCEDURE DoBullets;
  194. VAR temp      : PBullet;        { temporary bullet    }
  195.     next_Bull : PBullet;        { next bullet in list }
  196.     done      : boolean;        { bullet done?        }
  197. BEGIN
  198.  Temp:=FirstBullet;             { start with first bullet }
  199.  
  200.  WHILE Temp<>Nil do begin       { while not pointing to NIL, do bullets }
  201.        with Temp^ do begin
  202.             done:=false;        { bullet not yet done }
  203.             next_Bull:=temp^.next; { pointer to Next-bullet in list }
  204.  
  205.             Case AI of             { check on bullet AI }
  206.              1 : BEGIN             { normal bullet }
  207.                    inc(xpos,xspeed); { increase X position }
  208.                    inc(ypos,yspeed); { increase Y position }
  209.                    {
  210.                      ....
  211.                        Check boundaries of screen
  212.                        and check for wall-background blocks
  213.                      ....
  214.                    }
  215.                    { Bullet hits something, or is off-screen }
  216.                       if not EraseBullet(temp) then begin
  217.                          { ERROR! bullet could not be disposed }
  218.                          halt(1);
  219.                       end;
  220.                       Done:=True; { Bullet is done }
  221.                  END;
  222.             END;{CASE ai }
  223.  
  224.             { if bullet was not "erased" continue the procedure }
  225.             If Not done then begin { see if we hit the player }
  226.                  {
  227.                    ....
  228.                      See if bullet is not shot by player
  229.                    ....
  230.                  }
  231.                  {
  232.                    ....
  233.                     Check with player coordinates
  234.                    ....
  235.                  }
  236.                  { IF HIT PLAYER THEN BEGIN }
  237.                      { Bullet hits player, and is erased }
  238.                      if not EraseBullet(temp) then begin
  239.                         { ERROR! bullet could not be disposed }
  240.                         halt(1);
  241.                      end;
  242.                      done:=true;
  243.             end;
  244.             { if bullet was not "erased" continue the procedure }
  245.             if not done then
  246.                {
  247.                  ....
  248.                    Draw the image on the screen!
  249.                  ....
  250.                }
  251.        end;
  252.        temp:=next_Bull;  { get pointer to next bullet in the list }
  253.  end;
  254. END;
  255. {=-=-= End of Example =-=-=}
  256.  
  257. This should make the bullets fly around, BUT we DO check if the bullet
  258. hits something, but we should allso erase it from the linked-list.
  259. For erasing it we need the third procedure called ERASEBULLET.
  260.  
  261.  
  262. Kill, Kill, Kill the bullet
  263. ===========================-
  264.  
  265. Question: Why do we need to erase the bullet, can't we just leave it, and 
  266.     not draw it?
  267.  Answer : No, because it stopped existing, and we need the memory it
  268.           uses.
  269.              
  270. By erasing the bullet from the linked-list, you'll also free up the
  271. memory occupied by it and that is the power of linked-lists.
  272. So how do we erase it? Well let's see what ERASEBULLET does:
  273.  
  274.  * When calling it, specify the pointer to the BULLET
  275.  * Simply erase it from memory,
  276.  * and update the list-pointers.
  277.  
  278. {=-=-= Example =-=-=}
  279. FUNCTION EraseBullet(Temp:PBullet):boolean;
  280. VAR  next_bull,
  281.      prev_bull  : PBullet;
  282. BEGIN
  283.   EraseBullet:=false;           { bullet not yet erased }
  284.   if Temp=NIL then exit;        { if bullet=NIL then just exit }
  285.  
  286.   prev_BULL:=Temp^.prev;        { Correct the bullet pointers }
  287.   next_BULL:=Temp^.next;
  288.  
  289.   if prev_BULL=NIL then begin   { if there is no previous bullet, then }
  290.      FirstBullet:=next_Bull;    { we'r working with the firstbullet }
  291.   end else
  292.      Prev_bull^.next:=next_bull;
  293.  
  294.   if next_bull=NIL then begin   { if there is no next bullet , then }
  295.      LastBullet:=prev_Bull;     { we'r working with the lastbullet }
  296.   end else
  297.      Next_Bull^.prev:=prev_Bull;
  298.  
  299.   dispose(Temp);                { dispose the bullet, freeing up memory }
  300. END;
  301. {=-=-= End of Example =-=-=}
  302.  
  303. That's it!
  304. ==========-
  305.  
  306. Well this was the bullet-code in a fast way, but just take a look at
  307. the supplied example code, and you should be able to do it your self.
  308. If you got some questions about the bullets, just email me and I'll try to 
  309. answer you'r question.
  310.  
  311. The next Tech file is about handling aliens, objects and bonus things...
  312.  
  313. If you want to know about some other game-related code, mail me aswell,
  314. and I might create a new TECH file, trust me I'm really that nice!
  315.  
  316.  
  317. PeeBee - September 10th '97
  318.